/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using VSLangProj80;
using DaveSexton.DocProject.Configuration;

namespace DaveSexton.DocProject.Wizards
{
	public sealed partial class ChooseBuildEngineControl : UserControl
	{
		#region Public Properties
		public string SelectedBuildEngineProviderName
		{
			get
			{
				return (string) engineList.SelectedItem;
			}
		}

		public int BuildEngineProviderCount
		{
			get
			{
				return engineList.Items.Count;
			}
		}
		#endregion

		#region Private / Protected
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ChooseBuildEngineControl" /> class.
		/// </summary>
		public ChooseBuildEngineControl()
		{
			InitializeComponent();
		}
		#endregion

		#region Methods
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
		private void FillEngineList()
		{
			engineList.SuspendLayout();

			string defaultProviderName = null;

			foreach (BuildEngineProvider provider in DocProjectEnvironment.BuildEngineProviders)
			{
				try
				{
					provider.Verify();

					if (defaultProviderName == null || provider == DocProjectEnvironment.DefaultBuildEngineProvider)
						defaultProviderName = provider.Name;

					engineList.Items.Add(provider.Name);
				}
				catch (Exception ex)
				{
					Log.Exception(ex, Resources.Text.WizardInvalidProviderLog, provider.Name);

					MessageBox.Show(DocProjectEnvironment.MainWindow, string.Format(System.Globalization.CultureInfo.CurrentCulture,
						Resources.Text.WizardInvalidProvider, provider.Name, Environment.NewLine, ex.Message),
						Resources.Text.WizardErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 
						DocProjectEnvironment.MessageBoxOptions);
				}
			}

			if (defaultProviderName != null)
				engineList.SelectedItem = defaultProviderName;

			engineList.ResumeLayout(true);
		}
		#endregion

		#region Events
		private readonly object SelectedBuildEngineProviderNameChangedEvent = new object();

		/// <summary>
		/// Event raised after the <see cref="SelectedBuildEngineProviderName" /> property value has changed.
		/// </summary>
		[Category("Property Changed")]
		[Description("Event raised after the SelectedBuildEngineProviderName property value has changed.")]
		public event EventHandler SelectedBuildEngineProviderNameChanged
		{
			add
			{
				lock (SelectedBuildEngineProviderNameChangedEvent)
				{
					Events.AddHandler(SelectedBuildEngineProviderNameChangedEvent, value);
				}
			}
			remove
			{
				lock (SelectedBuildEngineProviderNameChangedEvent)
				{
					Events.RemoveHandler(SelectedBuildEngineProviderNameChangedEvent, value);
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="SelectedBuildEngineProviderNameChanged" /> event.
		/// </summary>
		/// <param name="e"><see cref="EventArgs" /> object that provides the arguments for the event.</param>
		private void OnSelectedBuildEngineProviderNameChanged(EventArgs e)
		{
			EventHandler handler = null;

			lock (SelectedBuildEngineProviderNameChangedEvent)
			{
				handler = (EventHandler) Events[SelectedBuildEngineProviderNameChangedEvent];
			}

			if (handler != null)
				handler(this, e);
		}
		#endregion

		#region Event Handlers
		protected override void OnLoad(EventArgs e)
		{
			FillEngineList();

			base.OnLoad(e);
		}

		private void engineList_SelectedIndexChanged(object sender, EventArgs e)
		{
			OnSelectedBuildEngineProviderNameChanged(EventArgs.Empty);
		}
		#endregion
	}
}
